package com.fick.tags;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.NewsDAO;
import com.fick.hibernate.model.News;
import com.fick.util.General;
/**
 * 显示信息内容
 * 
 * **/
@SuppressWarnings("serial")
public class NewsContentCommonTag extends TagSupport {
	private static Log log = LogFactory.getLog(NewsContentCommonTag.class);
	
	
	public int doStartTag() throws JspException {
		StringBuffer sb = new StringBuffer();
		NewsDAO dao=new NewsDAO();
		News news=null;
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		String newsidS = request.getParameter("newsid");
			
		if(!StringUtils.isEmpty(newsidS)){
			news = dao.findById(Integer.parseInt(newsidS));
		} 
		if(news == null) {
			sb.append("<p>there is not a right newsid</P>");
		} else {
			if (news!=null) {
				//增加点击量
				news.setClickNum(news.getClickNum()+1);
				dao.update(news);
				sb.append("<h1>");
				sb.append(news.getTitle());
				sb.append("</h1>");
				sb.append("<div>");
				sb.append(news.getContent());
				//附件
				if (news.getAttachment()!=null && news.getAttachment().trim().length()>0) {
					sb.append("<br>附件如下：<br>");
					String attach[]=news.getAttachment().split(";");
					for(int i=0;i<attach.length;i++)
					{
					sb.append("<a href='");

					sb.append(attach[i]);
					/*
					try {
						sb.append(java.net.URLEncoder.encode(attach[i],"UTF-8"));
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
					*/
					sb.append("' >");
					sb.append(attach[i].substring(attach[i].lastIndexOf("/")+1,attach[i].length()));
					sb.append("</a><br>");
					}
				}
				sb.append("</div>");
				
		}
		
			JspWriter out = pageContext.getOut();
			try {
				out.println(sb.toString());
				out.flush();
			} catch (IOException e) {
				log.warn("信息显示标签加载错误！",e);
			}
	}
		return SKIP_BODY;
	}
}
